package action.cart;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import vo.CartVO;

public class RemoveCartAction {
	
	private String pid;
	private int sumprice;
	
	public String getPid() {return pid;}
	public void setPid(String pid) {this.pid = pid;}
	public int getSumprice() {return sumprice;}
	public void setSumprice(int sumprice) {this.sumprice = sumprice;}
	
	@SuppressWarnings("unchecked")
	public String execute() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		ArrayList<CartVO> cart = (ArrayList<CartVO>) session.getAttribute("cart");

		for(int i=0; i<cart.size(); i++) {
			if(cart.get(i).getPid().equals(pid)) {
				cart.remove(i);
			}
		}
		
		sumprice = 0;
		for(int i=0; i<cart.size(); i++) {
			sumprice = sumprice+(cart.get(i).getTotalprice());
		}		
		session.setAttribute("sumprice", sumprice);
				
		return "success";
	}
}